﻿Namespace ShopWindow

    Module mShopWindow

        Private WithEvents ShopWin As GUI.MirWindow

        Public Enum ShopMode

            Main = 0
            BuyStack = 1
            BuyItems = 2

        End Enum

        Private mMode As ShopMode
        Private mShown As Boolean

        Public WithEvents btnCloseShop As GUI.MirButton
        Public WithEvents btnCloseItem As GUI.MirButton

        Public WithEvents btnBuy As GUI.MirButton
        Public WithEvents btnBuyStack As GUI.MirButton
        Public WithEvents btnSelectItem As GUI.MirButton

        Public WithEvents btnGridLeft As GUI.MirButton
        Public WithEvents btnGridRight As GUI.MirButton
        Public WithEvents btnGridBuy As GUI.MirButton

        Public NPC_ID
        Private lstDef As List(Of ShopItemInfo) = New List(Of ShopItemInfo)
        Private lstItems As List(Of ShopItemInfo) = New List(Of ShopItemInfo)

        Private ScrollPos As Integer
        Private MainDisplayItems = 8
        Private BuyDisplayItems = 4

        Private HiLiteIdx As Integer = 0
        Private SelectedIdx As Integer = 0
        Private SelectedToBuyIdx As Integer = 0
        Private SelectedItem As ShopItemInfo
        Private GridSelectedIdx As Integer = 0
        Private GridSelectedItem As ShopItemInfo


        Public WithEvents ShopScrollBar As GUI.MirScrollBar

        Private UpdatingItems As Boolean
        Private ItemsPage As Integer

        Public Sub Init()

            ShopWin = New GUI.MirWindow("ShopWin", 50, 50, 289, 389, True)

            btnCloseShop = New GUI.MirButton(ShopWin, "btnCloseShop", 0, 0, 0.0F, ImgLibs.GameInter, 0, 3036, 3036)
            btnCloseItem = New GUI.MirButton(ShopWin, "btnCloseItem", 0, 0, 0.0F, ImgLibs.GameInter, 0, 3036, 3036)

            btnBuy = New GUI.MirButton(ShopWin, "btnBuy", 0, 0, 0.0F, ImgLibs.GameInter, 0, 405, 406)
            btnSelectItem = New GUI.MirButton(ShopWin, "btnSelectItem", 0, 0, 0.0F, ImgLibs.GameInter, 0, 394, 395)
            btnBuyStack = New GUI.MirButton(ShopWin, "btnBuyStack", 364, 64, 0.0F, ImgLibs.GameInter, 0, 394, 395)

            btnGridLeft = New GUI.MirButton(ShopWin, "btnGridLeft", 310, 167, 0.0F, ImgLibs.GameInter, 0, 399, 400)
            btnGridRight = New GUI.MirButton(ShopWin, "btnGridRight", 398, 167, 0.0F, ImgLibs.GameInter, 0, 401, 402)
            btnGridBuy = New GUI.MirButton(ShopWin, "btnGridBuy", 354, 167, 0.0F, ImgLibs.GameInter, 0, 396, 397)

            ShopScrollBar = New GUI.MirScrollBar(ShopWin, "ShopScrollBar", 272, 25, 12, 295, 2, EvoLib.Protocol.ImgLibs.GameInter, 1226)

        End Sub

        Public Sub Shutdown()

            Hide()
            ShopWin.Destory()

        End Sub

        Public Sub StartDef(id As Long, isgrid As Boolean)

            If isgrid Then
                lstItems.Clear()
                GridSelectedIdx = -1
                GridSelectedItem = Nothing
            Else
                NPC_ID = id
                lstDef.Clear()
                SelectedIdx = -1
                SelectedItem = Nothing
                SelectedToBuyIdx = -1
                ScrollPos = 0
                ShopScrollBar.SetValue(0)
            End If

        End Sub

        Public Sub AddDef(info As ShopItemInfo)

            lstDef.Add(info)

        End Sub

        Public Sub AddItem(info As ShopItemInfo)

            lstItems.Add(info)

        End Sub

        Public Sub ClearItems()
            lstItems.Clear()
        End Sub

        Public Sub Show()

            If Not mShown Then
                ScrollPos = 0
                SelectedIdx = -1
                Windows.AddWindow(ShopWin, Windows.WindowType.Normal)
                mShown = True
            End If

            Inventory.Hide()
            Inventory.Show()

        End Sub

        Public Sub Hide()

            If mShown Then
                Windows.RemoveWindow(ShopWin)
                mShown = False
            End If

            Inventory.Hide()

        End Sub

        Public Sub SetMode(mode As ShopMode)

            mMode = mode
            Dim max As Integer

            Select Case mMode

                Case ShopMode.Main
                    ShopWin.SetSize(289, 389)
                    btnCloseShop.SetPos(259, 356)
                    btnCloseItem.Visible = False
                    btnSelectItem.Visible = False
                    btnBuy.Visible = True
                    btnBuy.SetPos(111, 354)
                    btnGridLeft.Visible = False
                    btnGridRight.Visible = False
                    btnGridBuy.Visible = False
                    btnBuyStack.Visible = False
                    SelectedIdx = -1
                    max = lstDef.Count - 7
                    ShopScrollBar.SetSize(12, 295)

                Case ShopMode.BuyStack
                    If SelectedIdx - ScrollPos > 3 Then
                        ScrollPos = SelectedIdx
                    End If
                    ShopWin.SetSize(513, 223)
                    btnCloseShop.SetPos(259, 189)
                    btnCloseItem.Visible = True
                    btnCloseItem.SetPos(481, 69)
                    btnSelectItem.Visible = True
                    btnSelectItem.SetPos(111, 187)
                    btnBuy.Visible = False
                    btnGridLeft.Visible = False
                    btnGridRight.Visible = False
                    btnGridBuy.Visible = False
                    btnBuyStack.Visible = True
                    max = lstDef.Count - 3
                    ShopScrollBar.SetSize(12, 128)

                Case ShopMode.BuyItems
                    If SelectedIdx - ScrollPos > 3 Then
                        ScrollPos = SelectedIdx
                    End If
                    ShopWin.SetSize(484, 224)
                    btnCloseShop.SetPos(259, 189)
                    btnCloseItem.Visible = True
                    btnCloseItem.SetPos(455, 172)
                    btnSelectItem.Visible = True
                    btnSelectItem.SetPos(111, 187)
                    btnBuy.Visible = False
                    btnGridLeft.Visible = True
                    btnGridRight.Visible = True
                    btnGridBuy.Visible = True
                    btnBuyStack.Visible = False
                    max = lstDef.Count - 3
                    ShopScrollBar.SetSize(12, 128)

            End Select

            If max < 1 Then max = 1
            ShopScrollBar.Maximum = max
            ShopScrollBar.SetValue(ScrollPos)

        End Sub

        Private Sub ShopWin_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles ShopWin.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                Select Case mMode

                    Case ShopMode.Main
                        With Libs.GameInter.Image(391)
                            .Draw(x - 110, y - 61, z)
                        End With

                    Case ShopMode.BuyStack
                        '254 16
                        With Libs.GameInter.Image(393)
                            .Draw(x - 254, y - 16, z)
                        End With

                    Case ShopMode.BuyItems
                        With Libs.GameInter.Image(392)
                            .Draw(x - 12, y - 16, z)
                        End With

                End Select

            End If

            If mode = Globals.DrawingMode.Normal Or mode = Globals.DrawingMode.Text Or mode = Globals.DrawingMode.Line Then

                Dim MaxItems As Integer

                If mMode = ShopMode.Main Then
                    MaxItems = MainDisplayItems
                Else
                    MaxItems = BuyDisplayItems
                End If

                Dim i As Integer = ScrollPos
                If lstDef.Count > 0 Then
                    If i >= lstDef.Count Then i = lstDef.Count - 1
                    Dim drawn As Integer
                    Do

                        If mode = Globals.DrawingMode.Normal Then

                            With Libs.Storeitem.Image(lstDef(i).Info.Base.INT_LOOKS)
                                .Draw(x + 39 - (.Header.Width / 2), y + (42 * drawn) + 36 - (.Header.Height / 2), z - 0.001F)
                            End With

                        ElseIf mode = Globals.DrawingMode.Text Then
                            DirectX.ProFont9.DrawBorderText(lstDef(i).Info.Base.STR_NAME & " - " & lstDef(i).Info.Base.INT8_COST & " gold", x + 76, y + (42 * drawn) + 30, Color.White, Color.Black, z - 0.001F)

                        ElseIf mode = Globals.DrawingMode.Line Then

                            If drawn = HiLiteIdx - 1 Then
                                Engine.DrawSolidBox(x + 20, x + 252, y + 16 + (42 * drawn), y + 53 + (42 * drawn), z, Color.FromArgb(128, Color.Yellow))
                            ElseIf i = SelectedIdx Then
                                Engine.DrawSolidBox(x + 20, x + 252, y + 16 + (42 * drawn), y + 53 + (42 * drawn), z, Color.FromArgb(128, Color.Gold))
                            End If

                        End If

                        drawn += 1
                        If drawn = MaxItems Then Exit Do
                        i += 1

                    Loop While i < lstDef.Count

                End If

                If mMode = ShopMode.BuyItems Then
                    If lstDef.Count > 0 Then
                        Dim tx As Integer
                        Dim ty As Integer

                        For Each item As ShopItemInfo In lstItems

                            If mode = Globals.DrawingMode.Normal Then
                                With Libs.Storeitem.Image(item.Info.Base.INT_LOOKS)
                                    .Draw(x + 328 + (tx * 38) - (.Header.Width / 2), y + 66 + (ty * 38) - (.Header.Height / 2), z - 0.001F)
                                End With
                            ElseIf mode = Globals.DrawingMode.Text Then
                                DirectX.ProFont9.DrawBorderText(item.Info.INT_REALCOST, x + 349 + (tx * 38) - DirectX.ProFont9.GetNormalTextWidth(item.Info.INT_REALCOST), y + 73 + (ty * 38), Color.White, Color.Black, z - 0.001F)
                            End If

                            tx += 1
                            If tx = 4 Then
                                tx = 0
                                ty += 1
                            End If


                        Next
                    End If

                    If mode = Globals.DrawingMode.Text Then
                        If UpdatingItems Then
                            DirectX.ProFont9.DrawBorderText("...", x + 387 - (DirectX.ProFont9.GetNormalTextWidth("...") / 2), y + 21, Color.White, Color.Black, z - 0.001F)
                        Else
                            DirectX.ProFont9.DrawBorderText(ItemsPage.ToString(), x + 387 - (DirectX.ProFont9.GetNormalTextWidth(ItemsPage.ToString()) / 2), y + 21, Color.White, Color.Black, z - 0.001F)
                        End If
                    End If

                    If mode = Globals.DrawingMode.Line And GridSelectedIdx <> -1 Then

                        Engine.DrawSolidBox(x + 309 + ((GridSelectedIdx Mod 4) * 38), x + 309 + ((GridSelectedIdx Mod 4) * 38) + 38, y + 47 + ((GridSelectedIdx \ 4) * 38), y + 47 + ((GridSelectedIdx \ 4) * 38) + 38, z, Color.FromArgb(128, Color.Gold))

                    End If

                ElseIf mMode = ShopMode.BuyStack Then
                    If lstDef.Count > 0 And SelectedToBuyIdx <> -1 Then
                        If mode = Globals.DrawingMode.Normal Then
                            With Libs.Storeitem.Image(SelectedItem.Info.Base.INT_LOOKS)
                                .Draw(x + 319 - (.Header.Width / 2), y + 36 - (.Header.Height / 2), z - 0.001F)
                            End With
                        ElseIf mode = Globals.DrawingMode.Text Then
                            DirectX.ProFont9.DrawBorderText(SelectedItem.Info.Base.STR_NAME & vbCrLf & lstDef(SelectedToBuyIdx).Info.Base.INT8_COST & " gold", x + 357, y + 25, Color.White, Color.Black, z - 0.001F)
                        End If
                    End If

                End If

            End If

        End Sub

        Private Sub btnCloseItem_Click() Handles btnCloseItem.Click
            SetMode(ShopMode.Main)
        End Sub

        Private Sub btnCloseShop_Click() Handles btnCloseShop.Click
            Hide()
        End Sub

        Private Sub ShopWin_MouseEvent(ByRef type As Mouse.MouseInputType, ByRef e As MouseEventArgs) Handles ShopWin.MouseEvent

            If type = Mouse.MouseInputType.MouseMove Then

                Dim rect As Rectangle = New Rectangle(20, 16, 232, 37)
                'rect.Offset(ShopWin.mPosX, ShopWin.mPosY)

                Dim max As Integer
                If mMode = ShopMode.Main Then
                    max = MainDisplayItems
                Else
                    max = BuyDisplayItems
                End If

                For i As Integer = 1 To max

                    If rect.Contains(e.X, e.Y) Then
                        HiLiteIdx = i
                        Exit Sub
                    End If

                    rect.Offset(0, 42)

                Next

                HiLiteIdx = -1

            ElseIf type = Mouse.MouseInputType.MouseClick Then

                If e.X >= 19 And e.X <= 253 And HiLiteIdx <> -1 Then
                    SelectedIdx = ScrollPos + HiLiteIdx - 1

                    If SelectedIdx >= 0 And SelectedIdx < lstDef.Count Then
                    Else
                        SelectedIdx = -1
                        SetMode(ShopMode.Main)
                    End If

                ElseIf mMode = ShopMode.BuyItems Then

                    If e.X > 309 And e.X < 309 + (4 * 38) And e.Y > 47 And e.Y < 47 + (3 * 38) Then

                        Dim idx As Integer = 4 * Math.DivRem((e.Y - 47), 38, Nothing)
                        idx += Math.DivRem((e.X - 309), 38, Nothing)

                        If idx >= lstItems.Count Then
                            GridSelectedIdx = -1
                            GridSelectedItem = Nothing
                        Else

                            GridSelectedIdx = idx
                            If idx < lstItems.Count Then
                                GridSelectedItem = lstItems(idx)
                            End If

                        End If

                    End If

                End If

            End If

        End Sub

        Private Sub ShopScrollBar_Changed() Handles ShopScrollBar.Changed
            ScrollPos = ShopScrollBar.Value
        End Sub

        Private Sub btnBuyStack_Click() Handles btnBuyStack.Click

            If SelectedItem.Info.Base.BOOL_STACKS Then
                MessageBoxes.AddMessageBox("BUYSTACK", "How many do you want?", MessageBoxes.MsgBoxButtons.Input, AddressOf MsgBoxResultCallback, 3, , "0123456789")
            Else
                'send buy 1 packet
                Dim info As Protocol.ShopItemBuyInfo
                info.BYTE_DEFTYPE = ShopDefType.StackItem
                info.INT_COUNT = 1
                info.ID = SelectedItem.Info.Base.INT_ID_ITEMBASE
                info.ID_NPC = NPC_ID

                Dim pkt As Packet
                pkt.PacketType = GamePacketType.NPCShopBuy
                pkt.PacketOpCode = PacketOpCode.Enquiry
                pkt.Username = Globals.sUsername

                ClientSock.SendData(MakePacket(pkt, info, Globals.Crypto))

            End If

        End Sub

        Public Sub MsgBoxResultCallback(ByRef result As MessageBoxes.MsgBoxResult)

            Select Case result.mName

                Case "BUYSTACK"
                    If result.mInput.Length > 0 Then
                        Dim count As Integer = CInt(result.mInput)
                        If count > 0 Then

                            'send buy packet

                            Dim info As Protocol.ShopItemBuyInfo
                            info.BYTE_DEFTYPE = ShopDefType.StackItem
                            info.INT_COUNT = count
                            info.ID = SelectedItem.Info.Base.INT_ID_ITEMBASE
                            info.ID_NPC = NPC_ID

                            Dim pkt As Packet
                            pkt.PacketType = GamePacketType.NPCShopBuy
                            pkt.PacketOpCode = PacketOpCode.Enquiry
                            pkt.Username = Globals.sUsername

                            ClientSock.SendData(MakePacket(pkt, info, Globals.Crypto))

                        End If
                    End If

            End Select

        End Sub

        Public Sub EndItemsUpdate()

            UpdatingItems = False

        End Sub

        Private Sub btnGridLeft_Click() Handles btnGridLeft.Click


            If Not UpdatingItems And ItemsPage > 1 Then

                ItemsPage -= 1

                RequestGridItems()

            End If

        End Sub

        Private Sub btnGridRight_Click() Handles btnGridRight.Click

            If Not UpdatingItems And ItemsPage < 15 Then

                ItemsPage += 1

                RequestGridItems()

            End If

        End Sub

        Private Sub RequestGridItems()

            'send request for grid items
            Dim info As Protocol.ShopLoadItemsInfo
            info.ID_NPC = NPC_ID
            info.INT_PAGE = ItemsPage
            info.ID_ITEMBASE = SelectedItem.Info.Base.INT_ID_ITEMBASE

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.NPCShopLoadItems
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            ClientSock.SendData(MakePacket(pkt, info, Globals.Crypto))

            UpdatingItems = True

        End Sub

        Private Sub btnGridBuy_Click() Handles btnGridBuy.Click

            If Not UpdatingItems Then
                If GridSelectedIdx <> -1 And GridSelectedIdx < lstItems.Count Then

                    Dim info As Protocol.ShopItemBuyInfo
                    info.BYTE_DEFTYPE = ShopDefType.GridItem
                    info.INT_COUNT = 1
                    info.ID = GridSelectedItem.Info.INT_ID
                    info.ID_NPC = NPC_ID

                    Dim pkt As Packet
                    pkt.PacketType = GamePacketType.NPCShopBuy
                    pkt.PacketOpCode = PacketOpCode.Enquiry
                    pkt.Username = Globals.sUsername

                    ClientSock.SendData(MakePacket(pkt, info, Globals.Crypto))
                    Application.DoEvents()

                    RequestGridItems()

                End If
            End If

        End Sub

        Private Sub btnSelectItem_Click() Handles btnSelectItem.Click

            If SelectedIdx <> -1 Then

                SelectedToBuyIdx = SelectedIdx
                SelectedItem = lstDef(SelectedIdx)

                With SelectedItem
                    If SelectedItem.BYTE_DEFTYPE = ShopDefType.StackItem Then
                        SetMode(ShopMode.BuyStack)
                    ElseIf SelectedItem.BYTE_DEFTYPE = ShopDefType.GridItem Then
                        SetMode(ShopMode.BuyItems)
                        'send request for grid items
                        Dim info As Protocol.ShopLoadItemsInfo
                        info.ID_NPC = NPC_ID
                        ItemsPage = 1
                        info.INT_PAGE = ItemsPage
                        info.ID_ITEMBASE = SelectedItem.Info.Base.INT_ID_ITEMBASE

                        Dim pkt As Packet
                        pkt.PacketType = GamePacketType.NPCShopLoadItems
                        pkt.PacketOpCode = PacketOpCode.Enquiry
                        pkt.Username = Globals.sUsername

                        ClientSock.SendData(MakePacket(pkt, info, Globals.Crypto))

                        UpdatingItems = True

                    End If
                End With

            End If

        End Sub

        Private Sub btnBuy_Click() Handles btnBuy.Click
            Call btnSelectItem_Click()
        End Sub
    End Module

End Namespace
